<?php
/*
+--------------------------------------------------------------------------
|   WeCenter [#RELEASE_VERSION#]
|   ========================================
|   by WeCenter Software
|   © 2011 - 2014 WeCenter. All Rights Reserved
|   http://www.wecenter.com
|   ========================================
|   Support: WeCenter@qq.com
|
+---------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);


if (!defined('IN_ANWSION'))
{
	die;
}

class ajax extends AWS_CONTROLLER
{

	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查

		$rule_action['actions'] = array(
			'user_info'
		);
        if (!$this->user_id)
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请登录!')));
        }
		if ($this->user_info['permission']['visit_people'])
		{
			$rule_action['actions'][] = 'user_actions';
			$rule_action['actions'][] = 'follows';
			$rule_action['actions'][] = 'topics';
		}

		return $rule_action;
	}

	public function setup()
	{
		$this->per_page = get_setting('contents_per_page');

		HTTP::no_cache_header();

	}

    /**
     * 添加购物车
     * 1.没有购物车信息就插入
     * 2.有购物车信息则是更新 cart_content 这个字段  向数组中插入值
     * 3.购物车为空的处理
     */
    public function add_cart_action()
    {

        $gid = intval($_POST['gid']);
        if(!$gid ){
            H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('错误的选择')));
        }

        if($cart_info = $this->model('cart')->get_cart_list_by_uid($this->user_id)){

            if(in_array($gid,$cart_info['cart_content']['gids'])){
                H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('已经添加过')));

            }

            if(!$cart_info['cart_content']){
                $cart_info['cart_content']=array('gids'=>[]);
            }

            array_push($cart_info['cart_content']['gids'],$gid);

            $update_data['cart_content'] = $cart_info['cart_content'];
            $this->model('cart')->update_cart_fields($update_data,$cart_info['cart_id']);

            H::ajax_json_output(AWS_APP::RSM(null, '1', AWS_APP::lang()->_t('添加成功')));

        }

        $this->model('cart')->insert_goods($this->user_id,array('gids'=>[$gid]));

        H::ajax_json_output(AWS_APP::RSM(null, '1', AWS_APP::lang()->_t('添加成功')));

    }

    /**
     * 删除购物车
     * 1.没有购物车信息就插入
     * 2.有购物车信息则是更新 cart_content 这个字段  向数组中插入值
     * 3.购物车为空的处理
     */
    public function del_cart_action()
    {
        $gid = intval($_POST['gid']);
        if(!$gid ){
            H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('错误的选择')));
        }

        if($cart_info = $this->model('cart')->get_cart_list_by_uid($this->user_id)){

            if(in_array($gid,$cart_info['cart_content']['gids'])){
//                H::ajax_json_output(AWS_APP::RSM(null, '-1', AWS_APP::lang()->_t('已经添加过')));


                $key = array_search($gid, $cart_info['cart_content']['gids']);
                if ($key !== false){
                    array_splice($cart_info['cart_content']['gids'], $key, 1);
                }

            $update_data['cart_content'] = $cart_info['cart_content'];
            $this->model('cart')->update_cart_fields($update_data,$cart_info['cart_id']);

            H::ajax_json_output(AWS_APP::RSM(null, '1', AWS_APP::lang()->_t('删除成功')));

            }

        }

        $this->model('cart')->insert_goods($this->user_id,array('gids'=>[$gid]));

        H::ajax_json_output(AWS_APP::RSM(null, '1', AWS_APP::lang()->_t('添加成功')));

    }

	public function follows_action()
	{


        switch ($_GET['type'])
		{
			case 'follows':
				$users_list = $this->model('follow')->get_user_friends($_GET['uid'], (intval($_GET['page']) * $this->per_page) . ", {$this->per_page}");
			break;

			case 'fans':
				$users_list = $this->model('follow')->get_user_fans($_GET['uid'], (intval($_GET['page']) * $this->per_page) . ", {$this->per_page}");
			break;
		}

		if ($users_list AND $this->user_id)
		{
			foreach ($users_list as $key => $val)
			{
				$users_ids[] = $val['uid'];
			}

			if ($users_ids)
			{
				$follow_checks = $this->model('follow')->users_follow_check($this->user_id, $users_ids);

				foreach ($users_list as $key => $val)
				{
					$users_list[$key]['follow_check'] = $follow_checks[$val['uid']];
				}
			}
		}

		TPL::assign('users_list', $users_list);

		TPL::output('people/ajax/follows');
	}

	public function topics_action()
	{
		if ($topic_list = $this->model('topic')->get_focus_topic_list($_GET['uid'], (intval($_GET['page']) * $this->per_page) . ", {$this->per_page}") AND $this->user_id)
		{
			$topic_ids = array();

			foreach ($topic_list as $key => $val)
			{
				$topic_ids[] = $val['topic_id'];
			}

			if ($topic_ids)
			{
				$topic_focus = $this->model('topic')->has_focus_topics($this->user_id, $topic_ids);

				foreach ($topic_list as $key => $val)
				{
					$topic_list[$key]['has_focus'] = $topic_focus[$val['topic_id']];
				}
			}
		}

		TPL::assign('topic_list', $topic_list);

		TPL::output('people/ajax/topics');
	}
}